﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBD_DAL.Entities;
using BBD_DAL.DBConnector;
using System.Data;
using MySql.Data.MySqlClient;

namespace BBD_DAL.DAO
{
	public class StateDao : IBaseDao<State>
	{
		private DBConnection dbConnector;
		private Dictionary<string, string> queries;

		public StateDao(string connectionString)
		{
			this.Initialize(connectionString);
		}

		public void Initialize(string connectionString)
		{
			this.dbConnector = new DBConnection(connectionString);
			queries = new Dictionary<string, string>();
			queries.Add("Get", "SELECT * FROM wojewodztwa WHERE wojewodztwo_id = @id;");
			queries.Add("Save", "UPDATE wojewodztwa SET wojewodztwo_nazwa = @name, wojewodztwo_id = @wid WHERE region_id = @id;");
			queries.Add("Insert", "INSERT INTO wojewodztwa (wojewodztwo_nazwa, region_id) VALUES (@name, @rid);");
			queries.Add("Delete", "DELETE FROM wojewodztwa WHERE id = @id;");
			queries.Add("DeleteAll", "DELETE FROM wojewodztwa; ALTER TABLE wojewodztwa AUTO_INCREMENT = 1;");
			queries.Add("List", "SELECT * FROM wojewodztwa;");
		}

		public State Get(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["Get"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			}, out data);

			List<State> states = this.Convert(data);

			return states != null ? states[0] : null;
		}

		public void Save(State entity)
		{
			this.dbConnector.RunQuery(this.queries["Save"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = entity.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@name", Value = entity.StateName.GetStateFullName().Replace("-",""), MySqlDbType = MySqlDbType.VarChar},
				new MySqlParameter(){ParameterName = "@rid", Value = entity.Region.Id, MySqlDbType = MySqlDbType.Int64}
			});
		}

		public State Insert(State entity)
		{
			long id;
			this.dbConnector.RunQuery(this.queries["Insert"], new MySqlParameter[]{				
				new MySqlParameter(){ParameterName = "@name", Value = entity.StateName.GetStateFullName().Replace("-",""), MySqlDbType = MySqlDbType.VarChar},
				new MySqlParameter(){ParameterName = "@rid", Value = entity.Region.Id, MySqlDbType = MySqlDbType.Int64}
			}, out id);

			entity.Id = id;

			return entity;
		}

		public void Delete(long id)
		{
			this.dbConnector.RunQuery(this.queries["Delete"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			});
		}

		public void Delete(State entity)
		{
			this.Delete(entity.Id);
		}

		public void Delete()
		{
			this.dbConnector.RunQuery(this.queries["DeleteAll"], null);
		}

		public List<State> List()
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["List"], null, out data);
			return this.Convert(data);
		}

		private List<State> Convert(DataTable data)
		{
			if (data == null || data.Rows == null || data.Rows.Count == 0)
				return null;

			List<State> states = new List<State>();
			foreach (DataRow row in data.Rows)
			{
				State state = new State();
				state.Id = Int64.Parse(row["wojewodztwo_id"].ToString());
				state.StateName = States.Unknown.GetState(row["wojewodztwo_nazwa"].ToString());
				state.Region = new Region() { Id = Int64.Parse(row["region_id"].ToString()) };
				states.Add(state);
			}
			return states;
		}
	}
}
